<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
	<head>
		<base href="<%=basePath%>" />
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
		<title>合同清单</title>
		<link rel="stylesheet" href="res/layui/css/layui.css">
		<script src="res/js/jquery-2.1.4.min.js" type="text/javascript"></script>
		<script src="res/layui/layui.js"></script>
	</head>
	<body style="padding: 40px 40px 0px 40px">
		<form class="layui-form" id="Pact">
			<table id="ta">
				<div class="layui-form-item">
					<label class="layui-form-label">供货商名称</label>
					<div class="layui-input-inline">
							<select class="select" name="applyName" id="applyName" lay-filter="apply">
							<option value="" >请选择供货商</option>
						</select>
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">合同名称</label>
					<div class="layui-input-block">
						<input type="text" id="pactTitle" name="pactTitle" lay-verify="required"
							placeholder="请输入合同名称" autocomplete="off" class="layui-input">
					</div>
				</div>
		
				<div class="layui-form-item">
					<label class="layui-form-label">合同编号</label>
					<div class="layui-input-block">
						<input type="text" id="pactNumber" name="pactNumber" lay-verify="required"
							placeholder="请输入合同编号" autocomplete="off" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">甲方名字</label>
					<div class="layui-input-block">
						<input type="text" id="partaName" name="partaName" lay-verify="required"
							placeholder="请输入甲方名字" autocomplete="off" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">乙方名字</label>
					<div class="layui-input-block">
						<input type="text" id="partbName" name="partbName" lay-verify="required"
							placeholder="请输入乙方名字" autocomplete="off" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">有效期</label>
					<div class="layui-input-block">
						<input type="text" id="pactEndtime" name="pactEndtime" lay-verify="required"
							placeholder="有效期" autocomplete="off" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<div class="layui-form-item layui-form-text">
					    <label class="layui-form-label">合同内容</label>
					    <div class="layui-input-block">
					      <textarea placeholder="请输入内容" class="layui-textarea" id="pactText" name="pactText"></textarea>
					    </div>
					</div>
				</div>
				
				 <div class="layui-form-item" pane="">
				    <label class="layui-form-label">类型:</label>
				    <div class="layui-input-block">
				      <input name="type" value="1" title="商品" checked="" type="radio">
				    </div>
  				</div>
				<input type="hidden" id="departBy" name="departBy" value="${staff.staffName }">
				
				<tr>
					
					<td>
						<div class="layui-form-item">
							<label class="layui-form-label">商品名称:</label>
						
							<div class="layui-input-block">
								<!-- <input type="text" name="medicine" id="medicine"> -->
								<select class="select" name="wareName" id="wareName">
									
								</select>
							</div>
						</div>
					</td>
					
					<td>
						<div>
							<input type="button" class="layui-btn layui-btn-normal" id="addcure"  name="addcure" value="增加">
						</div>
					</td>
					
				</tr>
		</table>
		
		<table id="tab" class="layui-table" >
			
			
			<input type="hidden" name="str" id="str">
			<tr class="text-c">
				<th>商品号码</th>
				<th>商品名称</th>
				<th>操作</th>
			</tr>
			 
		
		</table>
			
			<div class="layui-input-block">
		        <button class="layui-btn" lay-submit lay-filter="demo">立即提交</button>
		        <button type="reset" class="layui-btn layui-btn-primary">重置</button>
		    </div>
		</form>
	


		<script type="text/javascript">
			var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
			
			var pactId;
			
			$(function (){
				pactId = GetQueryString("pactId");
				if(pactId!=null){
					pacteId(pactId);
				}
			});
			
			var form;
			layui.use(['form'], function() {
				
				form = layui.form;
				//监听提交
				form.on('submit(demo)', function(data){
					
					var str = "" ; //需要封装的字符串
					var rows = $("#tab tr").length-1 ;//表格的总行数
					
					
					//表格中所有的值.每个单元格用#隔开,每行使用&隔开.
					for(i=0;i<rows;i++){
						$("#tab tr:gt(0):eq("+i+") td").each(function(){
							str += $(this).text()+"#";
							
							
						});
						str+="&";
						   
					}

					var str=$("#str").val(str);
					
					var data = $("#Pact").serialize();
					var url = "Pact/addOrUpdate.action";
					var loadIndex = layer.load();
					$.post(url, data, function(mes){
						parent.layer.msg(mes.mes);
						
						layer.close(loadIndex);//加载层关闭  
						parent.layer.close(index);
						if($("#pactId").val()!=""){
							$(".layui-laypage-skip .layui-laypage-btn",window.parent.document).click();//刷新父页面数据表格的当前页
						}else{
							parent.table.reload('pactId');
						}

					});
					return false;
				});
				
			});
			
			function pacteId(pactId){
				$.ajax({
					type: "POST",
					url: "Pact/pacteId.action",
					data: {"pactId":pactId},
					async: false,
					error: function(){
						layer.mes("发生未知错误，请稍后再试！");
					},
					success: function(obj){
						$("#pactId").val(obj.pactId);
						$("#pactTitle").val(obj.pactTitle);
						$("#applyName").val(obj.applyName);
						$("#pactNumber").val(obj.pactNumber);
						$("#partaName").val(obj.partaName);
						$("#partbName").val(obj.partbName);
						$("#pactEndtime").val(obj.pactEndtime);
					}
				});
			}
			
			
			
			/**
			得到URL栏的参数
			*/
			function GetQueryString(name){
			     var reg = new RegExp("(^|&)"+ name +"=([^&]*)(&|$)");
			     var r = window.location.search.substr(1).match(reg);
			     if(r!=null)return  unescape(r[2]); return null;
			}
			
			
			
			
			
			
			
			function del(obj) {
				var tr = obj.parentNode;
				var row = tr.rowIndex;
				$("#tab tr:eq(" + row + ")").remove();
				
			}


			$(function(){
				
				
				
				
				
				//同步异步
				$.ajaxSetup({
					async:false
				});	
				var url="<%=basePath%>Pact/queryApply.action";
				$.post(url,null,function(info){
					for(i=0;i<info.length;i++){
						$("#applyName").append("<option value="+info[i].applyId+" name='applyName'>"+info[i].applyName+"</option>");
					}
				});
				
				
				
				//添加商品
				$("#addcure").click(function(){
					
					var wareId=$("#wareName").val();
					//alert(id+number);
					var type=$("#type").val();
					
					
					var url="<%=basePath%>Pact/addWare.action";
					var data={"wareId":wareId};
					$.post(url,data,function(info){																																												
						//增加一行																
						var newRow="<tr class='text-c'><td name='wareId'>"+info.wareId+"</td><td name='wareName'>"+info.wareName+"</td><td class='delete_css' onclick='del(this);'><i class='layui-icon'>&#xe640;</i></td></tr>";
						$("#tab tr:last").after(newRow);
						showColor();
					},"json");


				});
				
				//鼠标放上去显示颜色
				  function showColor(){
				  	
					$("#tab tr:gt(0)").hover(
						function(){$(this).addClass("hover_bg")},
						function(){$(this).removeClass("hover_bg")}
					);
				  }
				
				
				
				
				
			});
				
			layui.use(['form', 'layedit', 'laydate'], function(){
				  var form = layui.form
				  ,layer = layui.layer
				  ,layedit = layui.layedit
				  ,laydate = layui.laydate;
				  var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
				  
				  //日期
				  laydate.render({
				    elem: '#date'
				  });
				  laydate.render({
				    elem: '#date1'
				  });
				  
				  //创建一个编辑器
				  var editIndex = layedit.build('LAY_demo_editor');
				 
				 
				  
				  
				    
				  form.on('select(apply)', function(data){
					  //document.getElementById("prolistTypeName").value="";
			          $.getJSON("<%=basePath%>ware/showAll.action?page=1&limit=999&applyId="+data.value, function(data){
			              var optionstring = "";
			              $.each(data.data, function(i,item){
			                  optionstring += "<option value=\"" + item.wareId + "\" >" + item.wareName + "</option>";
			              });
			              $("#wareName").html('<option value=""></option>' + optionstring);
			              form.render('select'); //这个很重要
			          });
				  });
				});
			
			
			
			
		</script>
	</body>
</html>